
import 'dart:async';

import 'package:data_plugin/bmob/table/bmob_object.dart';
import 'package:html/dom.dart';
import 'package:data_plugin/bmob/bmob.dart';
import 'package:html/parser.dart';
import 'package:http/http.dart' as http;
import 'package:uuid/uuid.dart';
void main()async{
  Bmob.init("https://api2.bmob.cn", "d6120f0280df61012ca8e02b96e3d3b1", "27fd92eaacf8881933b09af7a4d8dc30");
  int page = 1;
  String url = 'https://cbaigui.com/page/';//床公床母
  List<String> lists = List.generate(117, (i){
    return url + i.toString();
  });
  for (String url in lists) {
    Document dc = await netFetch(url);
    await saveAll(getUrls(dc).map((model){
      return model.save();
    }).toList());
  }



  await Future.delayed(Duration(seconds: 100000));
}

saveAll(List<Future> futures)async{
  try{
    for(Future f in futures){
      await f;
    }
    return true;
  }catch(e){
    return false;
  }
}

Future<Document> netFetch(String url)async{
  var rsp = await http.Client().get(Uri.parse(url));
  if(rsp.statusCode == 200){
    String body = rsp.body;
    return parse(body);
  }return null;
}

List<TitleModel> getUrls(Document document){
  return document.getElementsByClassName('post-title').map((nodes){
    String title = nodes.getElementsByTagName('a').first.text;
    String url = nodes.getElementsByTagName('a').first.attributes['href'];
    return TitleModel(title,url);
  }).toList();
}

class TitleModel extends BmobObject {
  String title;String url;

  TitleModel(this.title, this.url);

  @override
  Map getParams() {
    Map<String,String> map = {
      'title':this.title,
      'url':this.url,
      'objectId':this.objectId,
    };
    return map;
  }
}

//通过url保存model
class SaveModelByUrlUtils{
  save(String url)async{
    var rsp = await http.Client().get(Uri.parse(url));
    if(rsp.statusCode == 200){
      String body = rsp.body;
      Document document = parse(body);
      String title = parseTitle(document);
      List<String> pictures = parsePictures(document);
      String content = parseContent(document);
      String category = parseCategory(document);
      ZhiyaoModel model = ZhiyaoModel(url, pictures.join('||'), category, title, content);
      await model.save();
    }
  }

  String parseCategory(Document doc){
    return doc.getElementsByClassName('page-title').first.getElementsByTagName('a').first.text;
  }

  String parseContent(Document doc){
    Element element = doc.getElementsByClassName('entry').first;
    var ps = element.getElementsByTagName('p');
    return ps.map<String>((p){
      return p.text;
    }).toList().join('');
  }

  List<String> parsePictures(Document doc){
    return doc.getElementsByClassName('wp-block-image').map<String>((e){
      return e.getElementsByTagName('img').first.attributes['src'];
    }).toList();
//  var tag = el[0].getElementsByTagName('img');
//  var img=  tag.first.attributes['src'];//  return element.getElementsByClassName('wp-block-image').map((node){
//    print(node.toString());
//    return node.attributes['src'];
//  });
  }

  String parseTitle(Document doc){
    return doc.getElementsByClassName('post-title').firstWhere((node){
      return node.localName == 'h1';
    }).text;
  }
}



class ZhiyaoModel extends BmobObject{
  String url;//连接
  String pictureUrl;//图片链接 class = wp-block-image
  String category;//分类
  String title;//标题  hi post-title
  String content;
  String uuid = Uuid().v4();

  ZhiyaoModel(this.url, this.pictureUrl, this.category, this.title,
      this.content);

  @override
  Map getParams() {
    Map<String,dynamic> map = {
      'url':this.url,
      'pictureUrl':this.pictureUrl,
      'category':this.category,
      'title':this.title,
      'content':this.content,
      'uuid':this.uuid,
      objectId:objectId
    };
    return map;
  }


}